﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class HeTong_GongYePin_MaiMai : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {           
            String HeTongHao = Request.QueryString["HeTongHao"];
            String sql = "select 品名,规格,数量,单位,原料采购合同.单价,金额,长度,交货时间,交货数量 from 原料 inner join 原料采购合同 on 原料.ID=原料采购合同.原料ID where 合同编号='" + Request.QueryString["HeTongHao"]+"'";
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select 详细名称,地址,法定代表人,委托代理人,电话,开户银行,账号,邮政编码,税号 from 原料供货单位 inner join 原料采购合同 on 原料采购合同.供货单位ID=原料供货单位.ID where 原料采购合同.合同编号='" + Request.QueryString["HeTongHao"]+"'";
            SqlDataReader read = Cmd.ExecuteReader();
            if (read.Read())
            {
                if (read.IsDBNull(0))
                {
                    this.Label1.Text = "";
                }
                else
                {
                    this.Label1.Text = read.GetString(0);
                }
                if (read.IsDBNull(1))
                {
                    this.Label2.Text = "";
                }
                else
                {
                    this.Label2.Text = read.GetString(1);
                }
                if (read.IsDBNull(2))
                {
                     this.Label3.Text = "";
                }
                else
                {
                     this.Label3.Text = read.GetString(2);
                }
                if (read.IsDBNull(3))
                {
                    this.Label4.Text= "";
                }
                else
                {
                    this.Label4.Text= read.GetString(3);
                }
                if (read.IsDBNull(4))
                {
                    this.Label5.Text = "";
                }
                else
                {
                   this.Label5.Text = read.GetString(4);
                }
                if (read.IsDBNull(5))
                {
                    this.Label6.Text = "";
                }
                else
                {
                    this.Label6.Text = read.GetString(5);
                }
                if (read.IsDBNull(6))
                {
                    this.Label7.Text = "";
                }
                else
                {
                    this.Label7.Text = read.GetString(6);
                }
                if (read.IsDBNull(7))
                {
                    this.Label8.Text = "";
                }
                else
                {
                    this.Label8.Text = read.GetString(7);
                }
                if (read.IsDBNull(8))
                {
                    this.Label9.Text = "";
                }
                else
                {
                    this.Label9.Text = read.GetString(8);
                }             
            }
            read.Close();
            Cmd.Dispose();
            Conn.Close();
        }     
       
        public override void VerifyRenderingInServerForm(Control control)
        { 

        }
       
        public void ToWord(System.Web.UI.Control ctl, String filetype, String filename)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + Server.UrlEncode(filename) + ".doc");

            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //更改ContentType的值为ms-word即可实现导出到Word  
            HttpContext.Current.Response.ContentType = filetype;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword  
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Button.Visible = false;           
            ToWord(this.Table1,"application/ms-word ", "hetong");

        }
        Decimal sum = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

               if (e.Row.RowType == DataControlRowType.DataRow)
               {
                DataRowView myrows = (DataRowView)e.Row.DataItem;
                sum += Convert.ToDecimal(myrows[5].ToString());
               }
               syc.Dal.Sale.DecimalToUpper tomoney = new syc.Dal.Sale.DecimalToUpper();
               String money=tomoney.ToUpper(sum);
                if (e.Row.RowType == DataControlRowType.Footer)
                {

                    e.Row.Cells[0].Text = "合计人民币(大写)：" + money;
                    e.Row.Cells[0].ColumnSpan = 9;
                    e.Row.Cells[1].Visible = false;
                    e.Row.Cells[2].Visible = false;
                    e.Row.Cells[3].Visible = false;
                    e.Row.Cells[4].Visible = false;
                    e.Row.Cells[5].Visible = false;
                    e.Row.Cells[6].Visible = false;
                    e.Row.Cells[7].Visible = false;
                    e.Row.Cells[8].Visible = false;
                }              
          
        }        
    }
}
